Welcome back. Break and continue are two special kinds of
statements that can be used within while or for loops.
A break statement breaks out of whatever loop contains it.
So here, if we're within a loop and we encounter a break statement,
then Python is going to break out of the loop immediately.
So, this break statement is going to say that the program should jump
from here to here and it's going to
skip the rest of whatever is in the body of the
for loop and it's not even going to check the condition again.
A continue statement is similar.
A continue statement like
a break statement does skip whatever is in the rest of the loop.
So, it's not going to run this code,
but unlike a break statement which takes us to the bottom of the loop,
a continue statement instead says to continue at the top
of the loop and so it's going to check this condition once more.
So, let's see what break and continue statements do in code.
So, here we have a while loop,
this condition is true meaning it's always going to be, well, true.
So, what that means is that without a break statement,
then this is almost by definition going to be an infinite loop.
But, here in the body of the loop,
we first print out this,
this phrase will always print and then we call break and then we
say print does this phrase print and then here we print out,
we're done with the while loop.
So, I want you to think a little bit about what this code is actually going to print.
So, what I expect to happen is that when we run
this code even though this says while true,
this is only going to print out once because after this prints out,
then we break out of our while loop.
Then, we skip what's here because that comes after
the break and then we print out we're done with the while loop.
So, I expect this and this to print out.
Let's run our code to be sure that's the case.
So, you can see the only things that print out are this statement and this statement.
Now, what would happen if I replace the break with a continue.
So, when I run this code,
what I should expect is that it's going to get stuck in an infinite loop.
The reason is that, we are online to print out this phrase will always print and then
this continue statement jumps to the top of the loop and checks the condition again,
and here this condition again by definition is going to be true,
and so we're going to print this out again and then
continue and so we have an infinite loop and I expect
this phase will always print to read printed out
while a huge number of times before our program actually stops terminating,
and you can see when we look at our code,
but that's exactly what happened.
So, let's look at another example to see how a continue statement works.
So, here we have a slightly more complicated piece of code.
We have a number x,
which we set to zero,
and then we say while x is less than 10,
and as long as x is less than 10,
we print out we are incrementing x.
Now, what we do is we say,
if x is even so in other words if x modulo two is zero,
then we add three to the value of x.
So, x would jump from zero to three,
and then we say continue,
which takes us back to the top of this for loop Then,
we say, if x modulo three is zero,
in other words, if x is divisible by three,
then we assign x to be x's value plus five and almost regardless,
then we add one to x.
By the time we're done,
we print out done and we print out the value of x.
Now, let's run this code and CodeLens to see what happens.
So, again we start x is zero and we say while x is less than 10,
print out we're incrementing x.
Here x is divisible by two,
so x is going to jump from zero to three.
Now, again, when we hit this continue statement,
we're going to go to the top of this while loop.
So, we jump back up to the top,
we ask is three less than 10? Yes it is.
In this case three is not an even number,
so we check is it divisible by three,
which it is and so we add five to x and then we add one more on the x,
and so x ends up with the value nine,
nine is less than 10,
so we print out we're incrementing x,
nine is not even,
but it is divisible by three,
so x gets the value 14 and then 15 and then now when we check our condition,
15 is not less than 10,
so we say we're done with our loop and x has the final value of 15.
So again, here we use the continue statement
to ensure that we aren't going to run what was in the rest of this loop,
we instead jumped to the top of the while loop.
That's all for now, until next time.